//
//  NSDictionaryTest.m
//  iOS Sample Code
//
//  Created by lei yu on 11-12-8.
//  Copyright (c) 2011年 lancet. All rights reserved.
//

#import "NSDictionaryTest.h"

@implementation NSDictionaryTest

-(void)testNSDictionary{
    
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSMutableDictionary *glossary = [NSMutableDictionary dictionary];
    
    
    // Store three entries in the glossary
    [glossary setObject: @"A class defined so other classes can inherit from it" forKey: @"abstract class" ];
    [glossary setObject: @"To implement all the methods defined in a protocol" forKey: @"adopt"];
    [glossary setObject: @"Storing an object for later use" forKey: @"archiving"];
    // Retrieve and display them
    NSLog (@"abstract class: %@", [glossary objectForKey: @"abstract class"]);
    NSLog (@"adopt: %@", [glossary objectForKey: @"adopt"]);
    NSLog (@"archiving: %@", [glossary objectForKey: @"archiving"]);
    [pool drain];
    
    
}



-(void)testEnumerating{
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSDictionary *glossary =
    [NSDictionary dictionaryWithObjectsAndKeys:
     @"A class defined so other classes can inherit from it", @"abstract class",
     @"To implement all the methods defined in a protocol", @"adopt",
     @"Storing an object for later use", @"archiving",
     nil
     ];
    // Print all key-value pairs from the dictionary for ( NSString *key in glossary )
    
    for(NSString *key in glossary){
        NSLog (@"%@: %@",key,[glossary objectForKey:key]);        
    }
    
    
    NSArray *keys = [glossary allKeys];
    keys = [keys sortedArrayUsingComparator:
            ^(id obj1, id obj2) {
                return [obj1 compare: obj2];
            }];
    for (NSString *aState in keys){
        NSLog (@"State: %@ Capital: %@", aState, [glossary objectForKey: aState]);
    }
    
   NSEnumerator *enumer= [glossary keyEnumerator];
    for(NSString *key in enumer){
        NSLog(@"key is :%@",key);
    }
    
    enumer = [glossary objectEnumerator];
    for(NSString *obj in enumer){
        NSLog(@"object is:%@",obj);
    }
    
    
    [pool drain];
    
    
}
@end
